package com.magicodex.bookkeeping.biz.statistic.util;

import android.content.Context;

import com.magicodex.bookkeeping.biz.accounting.constant.AccountingConstants;
import com.magicodex.bookkeeping.biz.accounting.dto.AccountingDTO;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingJsonUtil;
import com.magicodex.bookkeeping.biz.common.constant.CollectionConstants;
import com.magicodex.bookkeeping.biz.common.util.CheckUtil;
import com.magicodex.bookkeeping.biz.common.util.CollectionUtil;
import com.magicodex.bookkeeping.biz.common.util.FileUtil;
import com.magicodex.bookkeeping.biz.common.util.StringUtil;
import com.magicodex.bookkeeping.biz.statistic.dto.StatisticDTO;
import com.magicodex.bookkeeping.ui.statistic.StatisticListViewAdapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mh.z
 */
public class StatisticDataUtil {

    /**
     * 处理数据变更
     *
     * @param context
     * @param adapter
     */
    public static void notifyDataSetChanged(Context context, StatisticListViewAdapter adapter) {
        CheckUtil.notNull(context, "context null");
        CheckUtil.notNull(adapter, "adapter null");

        // 更新列表
        adapter.notifyDataSetChanged();
    }

    /**
     * 加载统计记录
     *
     * @param context
     * @param startSymbol
     * @param endSymbol
     * @return
     */
    public static List<StatisticDTO> loadStatisticRecords(Context context, String startSymbol, String endSymbol) {
        CheckUtil.notNull(context, "context null");

        // 获取记账数据
        String json = FileUtil.readFile(context, AccountingConstants.DATA_PATH);
        if (StringUtil.isEmpty(json)) {
            return Collections.emptyList();
        }

        // 解析记账数据
        List<AccountingDTO> accountingList = AccountingJsonUtil.parseAccountingDTOs(json);
        if (CollectionUtil.isEmpty(accountingList)) {
            return Collections.emptyList();
        }

        Map<String, BigDecimal> statisticMap = new LinkedHashMap<>(CollectionConstants.DEFAULT_MAP_INITIAL_CAPACITY);
        // 计算统计数据
        accountingList.forEach((accounting) -> {
            String account = StringUtil.orEmpty(accounting.getAccount());
            String accountCategory = getAccountCategory(account, startSymbol, endSymbol);
            BigDecimal totalMoney = statisticMap.get(accountCategory);

            BigDecimal money = accounting.getMoney();
            if (money != null) {
                if (totalMoney != null) {
                    totalMoney = totalMoney.add(money);
                } else {
                    totalMoney = money;
                }
            }

            statisticMap.put(accountCategory, totalMoney);
        });

        List<StatisticDTO> statisticList = new ArrayList<>(statisticMap.size());
        statisticMap.forEach((accountCategory, totalMoney) -> {
            StatisticDTO statistic = new StatisticDTO();
            statistic.setAccountCategory(accountCategory);
            statistic.setTotalMoney(totalMoney);

            statisticList.add(statistic);
        });

        return statisticList;
    }

    /**
     * 获取账户类别
     *
     * @param account
     * @param startSymbol
     * @param endSymbol
     * @return
     */
    protected static String getAccountCategory(String account, String startSymbol, String endSymbol) {
        if (StringUtil.isEmpty(account)) {
            return "";
        }

        String accountCategory = account;
        int beginIndex = -1;
        int endIndex = -1;

        if (StringUtil.isNotEmpty(startSymbol)) {
            int startSymbolIndex = account.indexOf(startSymbol);

            if (startSymbolIndex >= 0) {
                beginIndex = startSymbolIndex + startSymbol.length();
            }
        } else {
            beginIndex = 0;
        }

        if (StringUtil.isNotEmpty(endSymbol)) {
            int endSymbolIndex = account.lastIndexOf(endSymbol);

            if (endSymbolIndex >= 0) {
                endIndex = endSymbolIndex;
            }
        } else {
            endIndex = account.length();
        }

        if (beginIndex >= 0 && endIndex >= 0) {
            accountCategory = account.substring(beginIndex, endIndex);
        }

        return accountCategory;
    }

}
